<template>
  <dynamic-table ref="tableRef" :columns="columns" :get-list-func="cipherList" rowKey="cipherid" :row-selection="rowSelection">
    <template v-slot:title>
      <a-button @click="addItem" v-permission="{ action: 'cipher:add', effect: 'disabled' }" type="primary">
        添加
      </a-button>
      <a-button @click="deleteItems" :disabled="isDisabled" type="primary">
        删除
      </a-button>
    </template>
  </dynamic-table>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, createVNode, computed, ref } from 'vue'
import { Modal } from 'ant-design-vue'
import { QuestionCircleOutlined } from '@ant-design/icons-vue'
import DynamicTable from '@/components/dynamic-table.vue'
import { useCreateModal } from '@/hooks'
import { cipherDelete, cipherList } from '@/api/strategy/cipher'
import AddModal from './add-modal.vue'
import { formatDate } from '@/utils/common'
import { columns } from './columns'
import { hasPermission } from '@/utils/hasPermission'
import { deleteMultiple } from '@/components/TableList'

export default defineComponent({
  name: 'cipher',
  components: {
    DynamicTable
  },
  setup() {
    const tableRef = ref<any>(null)

    const state = reactive({
      data: [],
      uploading: false,
      tableLoading: false,
      rowSelection: {
        onChange: (selectedRowKeys, selectedRows) => {
          state.rowSelection.selectedRowKeys = selectedRowKeys
        },
        selectedRowKeys: []
      }
    })

    // 删除多项
    const deleteItems = () => {
      Modal.confirm({
        title: '提示',
        icon: createVNode(QuestionCircleOutlined),
        content: '您确定要删除所有选中吗？',
        onOk: async () => {
          deleteMultiple(tableRef, cipherDelete, state.rowSelection.selectedRowKeys)
          state.rowSelection.selectedRowKeys = []
        }
      })
    }
    // 添加策略
    const addItem = () => {
      useCreateModal(AddModal, {
        callback: () => {
          tableRef.value.refreshTableData()
        }
      })
    }
    const isDisabled = computed(() => state.rowSelection.selectedRowKeys.length == 0 && hasPermission('cipher:delete'))

    return {
      ...toRefs(state),
      columns,
      tableRef,
      formatDate,
      cipherList,
      isDisabled,
      addItem,
      deleteItems
    }
  }
})
</script>
